import GameObject from "../GameObject";
import Utils from "../utils";
import GameConst from "../GameConst";

export default class BonusDestroyAnimation extends GameObject {
    constructor(x, y, name, cell) {
        super();
        var o = this;
        this.progress = 0;
        this.cell = cell;
        var u = Utils.createBitmap(name);
        u.pivotX = u.width / 2;
        u.pivotY = u.height / 2;
        u.x = x;
        u.y = y;
        GameConst.level.gemLayer.addChild(u);
        this.sprite = u;
        var glow = Utils.createBitmap("star_glow");
        glow.pivotX = glow.width / 2;
        glow.pivotY = glow.height / 2;
        glow.x = x;
        glow.y = y;
        glow.scaleX = glow.scaleY = .15;
        glow.alpha = 0;
        GameConst.level.underGemLayer.addChild(glow);
        this.glow = glow;
        Laya.Tween.to(this,{
            progress: 1
        }, GameConst.BonusDestroyAnimationTIME * 1e3, Laya.Ease.none,Laya.Handler.create(this,this.setLight));
    }

    onPlayComp(){
        if (this.cell){
            this.cell.tryPlayScoreAnimation();
        }
        this.destroy();
    }

    update(e) {
        if (this.sprite.visible) {
            this.sprite.scaleX = this.sprite.scaleY = Utils.lerp(1, 1.2, this.progress);
            this.glow.alpha = this.progress;
            this.glow.scaleX = this.glow.scaleY = Utils.lerp(.15, .95, this.progress);
        }
    }

    setLight () {
        this.sprite.visible = this.glow.visible = false;
        this.light = Utils.createAnimation("bonus_blink");
        this.light.x = this.sprite.x;
        this.light.y = this.sprite.y;
        this.light.scaleX = this.light.scaleY = GameConst.ANIM_SCALE * 1.5;
        // this.light.pivotX = 67 / GameConst.ANIM_SCALE;
        // this.light.pivotY = 61 / GameConst.ANIM_SCALE;
        GameConst.level.blockLayer.addChild(this.light);
        this.light.play(0,false);
        this.light.once(Laya.Event.COMPLETE,this,this.onPlayComp);
    }

    destroy() {
        super.destroy();
        Laya.Tween.clearAll(this);
        Utils.removeClip(this.glow);
        this.glow = null;
        Utils.removeClip(this.light);
        this.light = null;
    }
}

